/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.bath.environment;

/**
 *
 *  Fitness that could be in a range of values.
 *  This is useful if the fitness is calculated in iterations.
 *
 * @author pjl
 */
public class Fitness {

    public float maxFitness;
    public float minFitness;
    public float fitness;
    private boolean decided;
    public float target;

    public Fitness(float maxVal,float minVal) {
        maxFitness=maxVal;
        target=maxVal;
        fitness=-Float.MAX_VALUE;
        minFitness=minVal;
        decided=false;
    }

    public void addLowerBound(float x) {
        minFitness = Math.max(minFitness, x);

    }

    public void addUpperBound(float x) {
        maxFitness = Math.min(maxFitness, x);
    }

    public boolean decided() {
       return decided;
    }

    public void finish() {
        decided=true;
        fitness=maxFitness;
    }

    /**
     *
     * @return upper bound
     */
    public float getMaximumFitness() {
        return maxFitness;
    }
    

    public void set(int fitness) {
        assert(fitness <= maxFitness);
        assert(fitness >= minFitness);
        this.fitness=fitness;
        decided=true;
    }

    @Override
    public String toString() {
       if (decided) return String.format(" Fitness = %-9.3f",fitness);
       else return   String.format(" %-9.3f < Fitness < %-9.3f",minFitness,maxFitness);
    }

    public boolean success() {
        return fitness >= target;
    }
}
